<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TusTakimi
 *
 * @author Sedat Sevgili
 * 
 * tested in 25.12.2011 20:04
 */
class TusTakimi {
    //put your code here
    
    public static $qKlavyeHaritasi = array(
        array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '-'),
        array('q', 'w', 'e', 'r', 't', 'y', 'u', '\u0131', 'o', 'p', '\u11f', '\u00fc'),
        array('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\u015f', 'i', ',', '#'),
        array('z', 'x', 'c', 'v', 'b', 'n', 'm', '\u00f6', '\u00e7', '.', '.', '.')
    );
    
    public static $fKlavyeHaritasi = array(
        array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '-'),
        array('f', 'g', '\u011f', '\u0131', 'o', 'd', 'r', 'n', 'h', 'p', 'g', 'w'),
        array('u', 'i', 'e', 'a', '\u00fc', 't', 'k', 'm', 'l', 'y', '\u015f', 'x'),
        array('j', '\u00f6', 'v', 'c', '\u00e7', 'z', 's', 'b', '.', ',')
    );
    
    private $klavyeHaritasi = array(array());
    private $koordinatlar = array();
    
    private static $q = null;
    private static $f = null;
    
    function __construct($klavyeHaritasi) {
        $this->klavyeHaritasi = $klavyeHaritasi;
        for($i = 0; $i < count($klavyeHaritasi); $i++) {
            for($j = 0; $j < count($klavyeHaritasi[$i]); $j++) {
                $c = $klavyeHaritasi[$i][$j];
                $this->koordinatlar[$c] = new KarakterKoordinati($c, $i, $j);
            }
        }
    }
    
    public static function trQ() {
        if(is_null(self::$q)) {
            self::$q = new TusTakimi(self::$qKlavyeHaritasi);
        }
        return self::$q;
    }
    
    public static function trF() {
        if(is_null(self::$f)) {
            self::$f = new TusTakimi(self::$fKlavyeHaritasi);
        }
        return self::$f;
    }
    
    /**
     * @param mixed: ya karakter verilir ya da karakterin koordinatları verilir 2, 3 şeklinde.
     * @return type 
     */
    public function koordinat() {
        $args = func_get_args();
        if(count($args) == 1) {
            return $this->koordinatlar[$args[0]];
        } elseif (count($args) == 2) {
            return $this->koordinatlar[$this->klavyeHaritasi[$args[0]][$args[1]]];
        }
        throw new Exception("Geçersiz parametreler: " . print_r($args, true));
    }
    
    /**
     * Verilen iki karakter arasındaki klavye mesafesini getirir.
     * a-s arasındaki mesafeyi 10,  a-e arasındaki mesafeyi 22 verir.
     * sqrt((y2-y1)^2 + (x2-x1)^2))*10
     * 
     * @param char $c1
     * @param char $c2
     * @return int 
     */
    public function mesafeHesapla($c1, $c2) {
        $k1 = $this->koordinatlar[$c1];
        $k2 = $this->koordinatlar[$c2];
        if(is_null($k1) || is_null($k2)) {
            return -1;
        }
        $mesafeD = sqrt(
                floatval($k1->x - $k2->x) * ($k1->x - $k2->x) +
                floatval($k1->y - $k2->y) * ($k1->y - $k2->y)
        );
        $normalizeMesafe = intval($mesafeD * 10);
        return $normalizeMesafe;
    }
    
    public function __toString() {
        return print_r($this->koordinatlar, true);
    }
    
    public function karakter($i, $j) {
        return $this->klavyeHaritasi[$i][$j];
    }
}

class KarakterKoordinati {
    
    public $c, $x, $y;
    
    function __construct($c, $x, $y) {
        $this->c = $c;
        $this->x = $x;
        $this->y = $y;
    }
    
    public function __toString() {
        return "(" . $this->x . "," . $this->y . ") " . $this->c;
    }
}